<?php
declare(strict_types=1);
namespace App\Mail;

use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;
use Hyperf\Logger\LoggerFactory;

class WelcomeMailWithText
{
    protected $logger;
    private Mailer $mailer;

    public function __construct(LoggerFactory $loggerFactory)
    {
        $this->logger = $loggerFactory->get('log', 'default');
        // 从环境变量获取配置
        $host = env('MAIL_HOST', 'smtp.qq.com');
        $port = env('MAIL_PORT', 465);
        $username = env('MAIL_USERNAME');
        $password = env('MAIL_PASSWORD');
        $encryption = env('MAIL_ENCRYPTION', 'ssl');
        // 创建传输
        $dsn = "smtp://{$username}:{$password}@{$host}:{$port}?encryption={$encryption}";
        $this->logger->info("邮件服务配置:".$dsn);
        $transport = Transport::fromDsn($dsn);

        $this->mailer = new Mailer($transport);
    }
    public function sendChangeEmail(string $to, string $token): bool
    {
        try {
            $html = $this->changeEmail($token);

            $email = (new Email())
                ->from(env('MAIL_FROM_ADDRESS', 'hello@example.com'))
                ->to($to)
                ->subject('修改邮箱！')
                ->html($html);

            $this->mailer->send($email);
            return true;
        } catch (\Exception $e) {
            // 记录错误日志
            $this->logger->error("邮件发送失败:".$e->getMessage());

            return false;
        }
    }
    public function sendWelcomeEmail(string $to, string $name): bool
    {
        try {
            $html = $this->buildWelcomeHtml($name);

            $email = (new Email())
                ->from(env('MAIL_FROM_ADDRESS', 'hello@example.com'))
                ->to($to)
                ->subject('欢迎加入我们！')
                ->html($html);

            $this->mailer->send($email);
            return true;
        } catch (\Exception $e) {
            // 记录错误日志
            $this->logger->error("邮件发送失败:".$e->getMessage());

            return false;
        }
    }
    private function changeEmail()
    {
        return "
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset=\"utf-8\">
            <title>欢迎邮件</title>
            <style>
                body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
                .container { max-width: 600px; margin: 0 auto; padding: 20px; }
                .header { background: #4CAF50; color: white; padding: 10px 20px; text-align: center; }
                .content { padding: 20px; background: #f9f9f9; }
                .footer { text-align: center; margin-top: 20px; font-size: 12px; color: #666; }
            </style>
        </head>
        <body>
            <div class=\"container\">
                <div class=\"header\">
                    <h1>修改邮箱！</h1>
                </div>
            </div>
        </body>
        </html>
        ";
    }
    private function buildWelcomeHtml(string $name): string
    {
        return "
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset=\"utf-8\">
            <title>欢迎邮件</title>
            <style>
                body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
                .container { max-width: 600px; margin: 0 auto; padding: 20px; }
                .header { background: #4CAF50; color: white; padding: 10px 20px; text-align: center; }
                .content { padding: 20px; background: #f9f9f9; }
                .footer { text-align: center; margin-top: 20px; font-size: 12px; color: #666; }
            </style>
        </head>
        <body>
            <div class=\"container\">
                <div class=\"header\">
                    <h1>欢迎加入我们！</h1>
                </div>
                <div class=\"content\">
                    <p>亲爱的 <strong>{$name}</strong>，</p>
                    <p>感谢您注册我们的服务！我们很高兴您能加入我们。</p>
                    <p>您的账户已经成功创建，现在就可以开始使用我们的所有功能。</p>
                    <p>如果您有任何问题，请随时联系我们的客服团队。</p>
                    <p>祝您使用愉快！</p>
                </div>
                <div class=\"footer\">
                    <p>这是一封自动发送的邮件，请勿回复。</p>
                    <p>&copy; " . date('Y') . " Your Company. All rights reserved.</p>
                </div>
            </div>
        </body>
        </html>
        ";
    }
}